package elven.codingit.easysearch.util;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.FSDirectory;
import org.lionsoul.jcseg.analyzer.v5x.JcsegAnalyzer5X;
import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;

/**
 * Indexer的工具类
 * @author liuyy
 * @date 2016年8月26日
 */
public class IndexHelper {
	
	/**
	 * 默认的单个查询词:content
	 */
	private IndexHelper(){};
	
	/**
	 * 保存副本线程变量，减少查询对象实例化次数
	 */
	private static IndexHelper indexAnalyzer = null;
	private static ThreadLocal<IndexHelper> analyzerHolder = new ThreadLocal<IndexHelper>(){
		@Override
		protected IndexHelper initialValue() {
			if(indexAnalyzer == null){
				indexAnalyzer = new IndexHelper();
			}
			return indexAnalyzer;
		}
	};
	
	public static IndexHelper getInstance(){
		return analyzerHolder.get();
	}
	
	/**
	 * app文档索引存放位置,通过配置文件获取
	 */
	//private String appwordpath = "E:\\repository\\lucene\\indexer\\word";
	/**
	 * 打开app索引文档存储目录
	 * @throws Exception
	 * @author liuyy
	 * @date 2016年8月30日
	 */
	public FSDirectory openPaowordDirectory() throws Exception{
		//app文档索引存放位置
		Path repath = Paths.get("E:\\repository\\lucene\\indexer\\word");
		FSDirectory directory = FSDirectory.open(repath);
		return directory;
	}
	
	public boolean removePaowordDirectory(){
		File file = new File("E:\\repository\\lucene\\indexer\\word");
		return file.delete();
	}
	
	/**
	 * 给出分词器
	 * @author liuyy
	 * @throws Exception 
	 * @date 2016年8月30日
	 */
	public JcsegAnalyzer5X configAnalyzer() throws Exception{
		JcsegAnalyzer5X analyzer = new JcsegAnalyzer5X(JcsegTaskConfig.COMPLEX_MODE);
		JcsegTaskConfig config = analyzer.getTaskConfig();  
        config.setAppendCJKSyn(false);  
        config.setAppendCJKPinyin(false);  
        //config.load(getClass().getResourceAsStream("/jcseg.properties"));
        return analyzer;
	}
	/**
	 * 给出lucene查询
	 * @throws Exception
	 * @author liuyy
	 * @date 2016年8月30日
	 */
	public IndexSearcher configSearch(FSDirectory fsDirectory) throws Exception{
		IndexReader ireader = DirectoryReader.open(fsDirectory);
	    IndexSearcher isearcher = new IndexSearcher(ireader);
	    return isearcher;
	}
	
	/**
	 * 将key,value的值转换成field
	 * @author liuyy
	 * @date 2016年8月30日
	 */
	public Field getDataField(Object value, String key){
		Field field = new TextField(key, value.toString(), Field.Store.YES);
		/*if(value.getClass() == Date.class){
			field = new LongField(key, ((Date)value).getTime(), LongField.TYPE_STORED);
		}else if(value.getClass() == Integer.class){
			field = new IntField(key, (Integer)value, IntField.TYPE_STORED);
		}else if(value.getClass() == Long.class){
			field = new LongField(key, (Long)value, LongField.TYPE_STORED);
		}else if (value.getClass() == String.class) {
			field = new TextField(key, (String)value, Field.Store.YES);
		}*/
		return field;
	}
	
}